package com.itheima.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SetMealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetMealService.class)
@Transactional
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        /*
        * 1.添加套餐组的表
        * 2.通过添加套餐组返回的id，添加中间表检查组和套餐关系*/
       setMealDao.add(setmeal);
       setCheckGroupAndSetMeal(setmeal.getId(),checkgroupIds);

    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> setmeals = setMealDao.findByPage(queryString);

        return new PageResult(setmeals.getTotal(),setmeals.getResult());
    }

    public void setCheckGroupAndSetMeal(int Setmeal_id,Integer[] checkgroupIds){
            Map<String,Integer> map = new HashMap<>();
            map.put("setmeal_id",Setmeal_id);
        for (Integer checkgroupId : checkgroupIds) {
            map.put("checkgroupId",checkgroupId);
            setMealDao.setCheckGroupAndSetMeal(map);
        }
    }
    /*手机端*/

    @Override
    public List<Setmeal> findAll() throws Exception {
        List<Setmeal>list=setMealDao.findAll();
        return list;
    }

    @Override
    public Setmeal findById(Integer id) throws Exception {
        Setmeal  setmeal =  setMealDao.findById(id);
        return setmeal;
    }

    @Override
    public Setmeal findSetmealById(Integer id)  throws Exception{
        Setmeal  setmeal =  setMealDao.findSetmealById(id);
        return setmeal;

    }
}
